/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.server.modules.sys.service.loan;

import com.server.common.persistence.Page;
import com.server.common.service.CrudService;
import com.server.modules.sys.dao.loan.LoanTagsDao;
import com.server.modules.sys.entity.loan.LoanTags;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 产品标签管理Service
 * @author 王磊
 * @version 2016-09-09
 */
@Service
@Transactional(readOnly = true)
public class LoanTagsService extends CrudService<LoanTagsDao, LoanTags> {

	public LoanTags get(String id) {
		return super.get(id);
	}
	
	public List<LoanTags> findList(LoanTags loanTags) {
		return super.findList(loanTags);
	}
	
	public Page<LoanTags> findPage(Page<LoanTags> page, LoanTags loanTags) {
		return super.findPage(page, loanTags);
	}
	
	@Transactional(readOnly = false)
	public void save(LoanTags loanTags) {
		super.save(loanTags);
	}
	
	@Transactional(readOnly = false)
	public void delete(LoanTags loanTags) {
		super.delete(loanTags);
	}

	@Transactional(readOnly = false)
    public boolean deleteByLoanId(String id) {
		//参数校验
		if(StringUtils.isBlank(id)){
			return false;
		}
		return dao.deleteByLoanId(id)>0?true:false;
    }

	@Transactional(readOnly = false)
	public boolean saveByLoanId(String id, String[] tagsIds) {
		//参数校验
		if(StringUtils.isBlank(id)||tagsIds==null){
			return false;
		}
		//先删除之前的关系 再保存（避免出现重复插入的现象  这样效率稍微低一点，但这种操作不会太频繁，可以这样）
		this.deleteByLoanId(id);
		for (String tagsId : tagsIds) {
			LoanTags loanTags=new LoanTags();
			loanTags.setLoanId(id);
			loanTags.setTagId(tagsId);
			this.save(loanTags);
		}
		return true;
	}
}